<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 左边区域 -->
      <el-aside :width="collapse? '64px':'200px'">
        <div class="toggle-button" @click='toggle' >|||</div>
          <!-- 侧边栏菜单 -->
          <!-- 只有动态绑定后的数据才是布尔值 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            :unique-opened = 'true'
            :collapse='collapse'
            :collapse-transition="false"
            router
            :default-active= 'acticvePath'
            >
            <!-- 一级菜单 -->
            <!-- index相同的话点击一个菜单项,其余所有菜单项都会被展开 -->
            <el-submenu :index="item.id + '' " v-for='(item) in menuList' :key="item.id" >
              <!-- 一级菜单模板 -->
              <template slot="title">
                <!-- 图标 --> 
                <i :class="iconObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级子菜单 -->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="savedata('/' + subItem.path)"> 
                <!-- 二级菜单模板 -->
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
            
          </el-menu>
        </el-aside>
      <!-- 右侧主题区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
    data() {
        return {
            collapse:true,
            menuList: [],
            acticvePath: '',
            iconObj: {
              '125': 'iconfont icon-user',
              '103': 'iconfont icon-tijikongjian' ,
              '101': 'iconfont icon-shangpin',
              '102': 'iconfont icon-danju',
              '145': 'iconfont icon-baobiao',
            }
        }
    },
    created() {
        //发送axios请求, 获得 导航详情列表 数据
        this.getmMnuList()
        //并且获取高亮显示的二级菜单项
        this.acticvePath = window.sessionStorage.getItem('activePath')
    },
  methods: {
    logout: function () {
      //点击退出按钮以后,就清除 sessionstorage中的 token 值
      window.sessionStorage.clear();
      //跳转到登录页面
      this.$router.push("/login");
    },
    async getmMnuList() {
        const { data: res } = await this.$http.get('menus')
        console.log(res);
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.menuList = res.data
    },
    toggle: function () {
      //切换导航栏的状态 ( 折叠 还是 平铺 )
      this.collapse = !this.collapse

    },
    savedata: function (val) {
      //按键被点击以后, 就在sessionStorage中把当前被点击的路径保存
      window.sessionStorage.setItem('activePath', val)
      this.acticvePath = val
    }
  },
};
</script>


<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  background-color: #373d41;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .toggle-button {
    background-color: #4a5064;
    font-size: 12px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none ;
}
</style>